Laravel / Advanced / Cron Job
Cron Job
-
STEPS
1. Create cron file
create new file, app/Console/Commands/bithdaySend.php
namespace App\Console\Commands; use Illuminate\Console\Command; use DB; use App\Models\Seeker; class bithdaySend extends Command { protected $signature = 'birthday:send'; protected $description = 'Target send'; public function __construct() { parent::__construct(); } public function handle() { $expiryDate= \Carbon\Carbon::today()->subDays(7); $seeker=Seeker::where('created_at','<=',$expiryDate )->get(); } } 2.Schedule the cron
open \app\Console\Kernel.php
add below code in schedule()
$schedule->command('birthday:send')->hourly(); namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use App\Console\Command\bithdaySend; class Kernel extends ConsoleKernel { protected function schedule(Schedule $schedule): void { $schedule->command('birthday:send')->hourly(); } protected function commands(): void { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } } 3. Run using route
Route::get('send', function(){ return Artisan::call('birthday:send'); }); Route::get('/cron-prospect', function () { Artisan::call('target:targetNotification'); }); Route::get('/cron-installation', function () { Artisan::call('target:installationNotification'); }); 4. Run using CMD
php artisan birthday:send